package org.anyin.gitee.shiro.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.anyin.gitee.shiro.model.UserInfo;
import org.anyin.gitee.shiro.service.ITokenService;
import org.anyin.gitee.shiro.shiro.ShiroToken;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
@Slf4j
public class TokenServiceImpl implements ITokenService {

    private Map<String, UserInfo> tokenUserMap = new HashMap<>();

    @Override
    public boolean check(String token) {
        log.info("check token is valid");
        return true;
    }

    @Override
    public ShiroToken createToken(String token, UserInfo userInfo) {
        String targetToken = token;
        if(ObjectUtils.isEmpty(targetToken)){
            targetToken = UUID.randomUUID().toString();
        }
        // token 和 用户信息的映射，可以使用Redis存储 或者 替换为JWT
        tokenUserMap.put(targetToken, userInfo);
        return new ShiroToken(targetToken);
    }

    @Override
    public ShiroToken createToken(UserInfo userInfo) {
        return this.createToken("", userInfo);
    }

    @Override
    public UserInfo getUserInfo(String token) {
        return tokenUserMap.get(token);
    }
}
